package com.zx.web.dao.impl;

import com.zx.bean.Goods;
import com.zx.utils.DataSourceUtils;
import com.zx.web.dao.AdminGoodsDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 章鑫
 * @version V1.0
 * @Project 2022-09-05
 * @Package com.zx.web.dao.impl
 * @Description:
 * @Date 2022/9/16 15:45
 */
public class AdminGoodsDaoImpl implements AdminGoodsDao {
    @Override
    public List<Goods> getGoodsList() {
        QueryRunner qr = new QueryRunner();
        String sql = "select * from tb_goods";
        try {
            return qr.query(DataSourceUtils.getConnection(),sql,new BeanListHandler<>(Goods.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void addGoods(Goods goods) {
        QueryRunner qr = new QueryRunner();
        String sql = "INSERT INTO `tb_goods` (`id`, `name`, `pubdate`, `picture`, `price`, `star`, `intro`, `typeid`) VALUES (null, ?,?,?,?,?,?,?);";
        try {
            qr.update(DataSourceUtils.getConnection(),sql,goods.getName(),goods.getPubdate(),goods.getPicture(),goods.getPrice(),
                    goods.getStar(),goods.getIntro(),goods.getTypeid());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
